﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FreeDream.Common.Other
{
    /// <summary>
    /// 错误信息
    /// </summary>
    public static class DeviceMessages
    {
        public static Dictionary<string, ResultParam> dicDeviceStatus = new Dictionary<string, ResultParam>();
        public static ResultParam ChangeResultParam(ResultParam resultParam, string deviceName)
        {
            if (string.IsNullOrWhiteSpace(deviceName))
            {
                return resultParam;
            }

            var lstSettingItemAll = GetAllItem();

            if (lstSettingItemAll == null || !lstSettingItemAll.Any())
            {
                return resultParam;
            }

            lstSettingItemAll.ForEach(m =>
            {
                m.name = m.name?.ToLower();
            });

            var settingFilter = lstSettingItemAll.FirstOrDefault(m => m.name == deviceName.ToLower() && m.code == resultParam.code.ToString());

            if (settingFilter == null)
            {
                return resultParam;
            }

            if (!string.IsNullOrWhiteSpace(settingFilter.outcode))
            {
                if (int.TryParse(settingFilter.outcode, out int errcode))
                {
                    resultParam.code = errcode;
                }
            }

            if (!string.IsNullOrWhiteSpace(settingFilter.message))
            {
                resultParam.message = settingFilter.message;
            }

            dicDeviceStatus[settingFilter.name.ToLower()] = resultParam;

            return resultParam;
        }

        /// <summary>
        /// 设置信息
        /// </summary>
        /// <returns></returns>
        public static List<SettingItem> GetAllItem()
        {
            SettingConfig settingConfig = new SettingConfig();
            string configFileName = "DeviceMessages.xml";
            var deviceSetting = settingConfig.GetConfig<DeviceSetting>(configFileName);

            //List<SettingItem> lstAll = deviceSetting.IDCardRead
            //                            .Union(deviceSetting.IDCardScanners)
            //                            .Union(deviceSetting.SignPads)
            //                            .Union(deviceSetting.FaceAntiSpoofings)
            //                            .Union(deviceSetting.HighCameras)
            //                            .Union(deviceSetting.Fingerprints)
            //                            .Where(m => !string.IsNullOrEmpty(m.name))
            //                            .Distinct().ToList();

            var lstAll = deviceSetting?.SettingItems;

            return lstAll;

        }

        public static void AddDeviceStatus()
        {
            var resultparam0 = new ResultParam { code = 0, message = "qwe" };
            var resultparam1 = new ResultParam { code = 5001, message = "蜘蛛侠c" };
            var resultparam2 = new ResultParam { code = 5002, message = "ddd" };
            dicDeviceStatus[DeviceEnum.IDCardScanner.ToString()] = resultparam0;
            dicDeviceStatus[DeviceEnum.SignPad.ToString()] = resultparam1;
            dicDeviceStatus[DeviceEnum.FaceAntiSpoofing.ToString()] = resultparam2;
        }

    }


    public class DeviceSetting
    {
        //public List<SettingItem> IDCardReads { get; set; } = new List<SettingItem>();
        //public  List<SettingItem> SignPads { get; set; } = new List<SettingItem>();
        //public List<SettingItem> FaceAntiSpoofings { get; set; } = new List<SettingItem>();
        //public List<SettingItem> HighCameras { get; set; } = new List<SettingItem>();
        //public List<SettingItem> Fingerprints { get; set; } = new List<SettingItem>();
        //public List<SettingItem> IDCardScanners { get; set; } = new List<SettingItem>();

        public List<SettingItem> SettingItems { get; set; } = new List<SettingItem>();

    }

    /// <summary>
    /// 错误项
    /// </summary>
    public class SettingItem
    {
        public string name { get; set; }

        public string type { get; set; }

        public string code { get; set; }

        public string message { get; set; }

        public string outcode { get; set; }
    }

    public enum DeviceEnum
    {
        Unknown = 0,

        /// <summary>
        /// 身份证阅读器
        /// </summary>
        IDCardRead,

        /// <summary>
        /// 签字版
        /// </summary>
        SignPad,

        /// <summary>
        /// 摄像头
        /// </summary>
        FaceAntiSpoofing,

        /// <summary>
        /// 高拍仪
        /// </summary>
        HighCamera,

        /// <summary>
        /// 指纹仪
        /// </summary>
        Fingerprint,

        /// <summary>
        /// 身份证
        /// </summary>
        IDCardScanner,

        /// <summary>
        /// 出入境
        /// </summary>
        VisaCard,
    }

    /// <summary>
    /// 暂时不用
    /// </summary>
    public enum ResultParamCode
    {
        Sucess = 0,

        FAIL = 1,

        EORRO = -1,

        NO_DATA = -2,

        /// <summary>
        /// 未知错误
        /// </summary>
        Exception = 9999,
        /// <summary>
        /// 设备未就绪
        /// </summary>
        DeviceNotReady = 5001,
        /// <summary>
        /// Tcp通信出错
        /// </summary>
        TcpError = 5002,
        /// <summary>
        /// 超时
        /// </summary>
        TimeOut = 5003,
        /// <summary>
        /// 命令返回失败
        /// </summary>
        CmdError = 5004,
        /// <summary>
        /// 机器回调反馈失败（机器已成功接收操作命令，但是执行过程中发生错误）
        /// </summary>
        CallBackError = 5005,

        /// <summary>
        /// 设备重启
        /// </summary>
        DeviceRestart = 5006
    }
}
